package cn.com.utt.unms.controller;

import java.security.Principal;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;


import cn.com.utt.common.vo.RestResult;
import cn.com.utt.common.vo.TableInput;
import cn.com.utt.common.vo.TableOutput;
import cn.com.utt.common.vo.User;
import cn.com.utt.unms.pojo.PageList;
import cn.com.utt.unms.service.AuthPageService;
import cn.com.utt.unms.vo.ImageUploadInfo;

@RestController
@RequestMapping("v1/authPage")
public class AuthPageController {
	@Autowired
	AuthPageService authPageService;
	
	/**
	 * 认证页面列表
	 * @param input
	 * @param principal
	 * @return
	 */ 
//	@PreAuthorize("hasRole('AUTH_VIEW')")
	@RequestMapping(value = "/list", method = { RequestMethod.POST }, consumes = "application/json")
	public Map<String, Object> queryAuthPageList(@RequestBody TableInput input,Principal principal){
		RestResult result = new RestResult();
		Authentication authentication = (Authentication) principal;
		User user = (User) authentication.getPrincipal();
		TableOutput<PageList> output=authPageService.queryAuthPageList(input, user.getId());
		result.put("start", output.getStart());
		result.put("rows", output.getRows());
		result.put("total", output.getTotal());
		result.put("page", output.getList());
//		result.put("page", output.getList().get(0).getPage());
//		result.put("pub", output.getList().get(0).getPub());
//		if (user.hasRole("ROLE_AUTH_EDIT")) {
//			result.put("hasEditRole", true);
//		} else {
//			result.put("hasEditRole", false);
//		}
		System.out.println(result.toJsonObject());
		return result.toJsonObject();
	}
	/**
	 * 删除
	 * @param pageId
	 * @param principal
	 * @return
	 */
	@RequestMapping(value = "/delAuthPage", method = { RequestMethod.POST })
	@ResponseBody
	public  Map<String, Object> delAuthPage(@RequestParam(value = "pageIds[]")List<Long> pageIds,Principal principal){
		Authentication authentication = (Authentication) principal;
		User user = (User) authentication.getPrincipal();
		RestResult result = new RestResult();
		result = authPageService.delAuthPages(pageIds,user.getId());
		return result.toJsonObject();
	}
	/**
	 * 编辑--初始化
	 * @param pageId此处值为pageList_id
	 * @param principal
	 * @return
	 */
	@RequestMapping(value = "/loadUserDefinedPage", method = { RequestMethod.POST })
	public  Map<String, Object> loadUserDefinedPage(Long pageId,Principal principal){
		Authentication authentication = (Authentication) principal;
		User user = (User) authentication.getPrincipal();
		RestResult result = new RestResult();
		result = authPageService.loadUserDefinedPage(pageId,user.getId());
		return result.toJsonObject();
	} 
	/**
	 * 上传图片
	 * @param uploadFileName
	 * @param type
	 * @param upload
	 * @param principal
	 * @return
	 */
	@RequestMapping(value = "/uploadImage", method = { RequestMethod.POST })
	public  Map<String, Object> uploadImage(@RequestParam("file") MultipartFile multipartfile,@RequestParam("type") String type,Principal principal){
		RestResult result = new RestResult();
		result = authPageService.uploadImage(multipartfile,type);
		return result.toJsonObject();
	} 
	/** 
	 * 保存图片
	 * @param imageUploadInfo
	 * @param principal
	 * @return
	 */
	@RequestMapping(value = "/addUDImage", method = { RequestMethod.POST }, consumes = "application/json")
	public  Map<String, Object> addUDImage(@RequestBody ImageUploadInfo imageUploadInfo,Principal principal){
		Authentication authentication = (Authentication) principal;
		User user = (User) authentication.getPrincipal();
		RestResult result = new RestResult();
		result = authPageService.addUDImage(imageUploadInfo,user.getId());
		return result.toJsonObject();
	}
	/**
	 * 新建与编辑
	 * @param mainId
	 * @param loginContent
	 * @param successContent
	 * @param type
	 * @param action
	 * @param pageTitle
	 * @param pageName
	 * @param principal
	 * @return
	 */
//	@PreAuthorize("hasRole('AUTH_EDIT')")
	@RequestMapping(value = "/saveUserDefinedPage", method = { RequestMethod.POST })
	public  Map<String, Object> saveOrUpdatePage(Long pageId,String loginContent,String successContent,String pcLoginContent,String pcSuccessContent,int type,String action,String pageTitle,String pageName,Principal principal){
		Authentication authentication = (Authentication) principal;
		User user = (User) authentication.getPrincipal();
		RestResult result = new RestResult();
		result = authPageService.savePage(pageId,loginContent,successContent,pcLoginContent,pcSuccessContent,type,action,pageTitle,pageName,user.getId());
		return result.toJsonObject();
	}
	
}
